package com.chencs.biz.model.controller;

import com.alibaba.fastjson.JSON;
import com.chencs.biz.model.service.ModelService;
import com.chencs.vo.biz.model.ModeBlockModel;
import com.chencs.vo.biz.model.ModeModel;
import framework.model.ResponseOptUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by fangzehua on 2017/7/8.
 */
@RestController
@RequestMapping("/model")
public class ModelController {

    @Autowired
    private ModelService modelService;

    @RequestMapping(value = "/getModelList", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String getModeList(){
        List<ModeModel> list = modelService.getModelList();
        if(!list.isEmpty()){
            return JSON.toJSONString(list);
        }else{
            return ResponseOptUtil.commonMsgError("获取模块列表失败");
        }
    }

    @RequestMapping(value = "/findModelById", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String findModelById(@RequestBody String id){
        ModeModel modeModel = modelService.findModel(id);
        if(modeModel!=null){
            return JSON.toJSONString(modeModel);
        }else{
            return ResponseOptUtil.commonMsgError("获取模块失败");
        }
    }

    @RequestMapping(value = "/insertModel", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String insertModel(@RequestBody ModeModel modeModel){
        try {
            modelService.insertModel(modeModel);
            return  ResponseOptUtil.commonSucess();
        }catch (Exception e){
            return ResponseOptUtil.commonMsgError("添加模块失败");
        }
    }

    @RequestMapping(value = "/updateModel", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String updateModel(@RequestBody ModeModel modeModel){
        try {
            modelService.updateModel(modeModel);
            return  ResponseOptUtil.commonSucess();
        }catch (Exception e){
            return ResponseOptUtil.commonMsgError("修改模块失败");
        }
    }


    @RequestMapping(value = "/delModel", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String delModel(@RequestBody String  id){
        try {
            modelService.delModel(id);
            return  ResponseOptUtil.commonSucess();
        }catch (Exception e){
            return ResponseOptUtil.commonMsgError("刪除模块失败");
        }
    }

    @RequestMapping(value = "/updateBlock", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String updateBlock(@RequestBody ModeBlockModel modeModel){
        try {
            modelService.updateBlock(modeModel);
            return  ResponseOptUtil.commonSucess();
        }catch (Exception e){
            return ResponseOptUtil.commonMsgError("参数修改失败");
        }
    }



    @RequestMapping(value = "/updateParameter", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String updateParameter(@RequestBody ModeBlockModel modeBlockModel){
        System.out.println(JSON.toJSON(modeBlockModel));
        try {
            modelService.updateParameter(modeBlockModel);
            return  ResponseOptUtil.commonSucess();
        }catch (Exception e){
            return ResponseOptUtil.commonMsgError("参数设置错误");
        }
    }


    @RequestMapping(value = "/findModeBlockModelByMid", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String findModeBlockModelByMid(@RequestBody String mid) {
        ModeBlockModel modeBlockModel = modelService.findModeBlockModelByMid(mid);
        if(modeBlockModel!=null){
            return JSON.toJSONString(modeBlockModel);
        }else{
            return ResponseOptUtil.commonMsgError("获取模块參數失败");
        }
    }


}
